<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

if (isset($data['teamid']) and $data['teamid']!="") {
	$currentteam = $data['teamid'];
	$tm->assign('teamid',$data['teamid']);	
} else {
	$currentteam = $CURRENT_USER->teamid;
}

if (!isset($data['forder'])) {
	$data['forder']="asc";
} else {
	if ($data['op']=="order") {
		$data['forder'] = $data['forder']=="asc" ? "desc" : "asc"; 
	}
}

if (SAVE_SESSION == 'true') {
	saveCookie('poat['.$CURRENT_USER->userid.'][prj'.$_SESSION["projectid"].'][fstatusid]',$data['fstatusid']);
	saveCookie('poat['.$CURRENT_USER->userid.'][prj'.$_SESSION["projectid"].'][fuserid]',$data['fuserid']);
	saveCookie('poat['.$CURRENT_USER->userid.'][prj'.$_SESSION["projectid"].'][fpath]',$data['fpath']);
	saveCookie('poat['.$CURRENT_USER->userid.'][prj'.$_SESSION["projectid"].'][forder]',$data['forder']);
	saveCookie('poat['.$CURRENT_USER->userid.'][recperpage]',$data['recperpage']);
}

$files = new files($_SESSION['projectid']);
$files->statusid = $data['fstatusid'];
$files->userid = $data['fuserid'];
$files->subprj = $data['fpath'];
$files->teamid = $currentteam;
$files->order = $data['forder'];
$files->totalrecord = $data['recperpage'];
$files->record = $data['currentpage']-1;
$files->st=$data['ffilestate'];

/* IF IS FILTERED BY USER ONLY SHOW ASSIGNED FILES*/
if ($files->userid!='' and ($files->statusid!='' and ($files->statusid != FILE_STATUS_LOCKED AND $files->statusid != FILE_STATUS_PROOF))) {
	$list = array();
	$totalrecords = 0;
} else {
	if ($files->userid !='') {
		if ($files->statusid=='') {
			$files->statusid = array(FILE_STATUS_LOCKED,FILE_STATUS_PROOF);	
		}
	}
	$list = $loader->loadListBy($files,$files->loadfiltered(false,$data['fmyfiles']=="true" ? $CURRENT_USER->userid : ""));
	$totalrecords = $loader->fetchcount($files->loadfiltered(true,$data['fmyfiles']=="true" ? $CURRENT_USER->userid : ""));
}
/*
if ($data['fmyfiles']=="true") {
	$rlist = $list;
	$list = array();
	
	$files = new files($_SESSION['projectid']);
	$files->statusid = FILE_STATUS_LOCKED;
	$files->userid = $CURRENT_USER->userid;
	$tlist = $loader->loadListBy($files,$files->listMyFiles());
	foreach ($rlist as $r) {
		if (array_key_exists($r->id,$tlist)) {
			$list[$r->id]=$r;
		}
	}
}
/*
if ($data['ffilestate']!="") {
	$rlist = $list;
	$list = array();
	
	foreach ($rlist as $r) {
		if ($r->state()==$data['ffilestate']) {
			$list[$r->id]=$r;
		}
	}
}
*/
$flist = array();

$prj = new project();
$prj->id=$_SESSION['projectid'];
$loader->get($prj);

if ($prj->statsupdated!=null) {
	$ar=array(util::fromUTC($prj->statsupdated,$CURRENT_USER->timezone));
	$pmessage = createMessage($LANG['statsupdated'],$ar);
}

$sprjlist = $loader->loadListBy($prj,$prj->listSubProjects());

$team = new team;
$team->id = $currentteam;

$loader->get($team);

$tuns = 0;
$tfuz = 0;
foreach ($list as $key => $obj) {
	$obj->lastupdate = util::fromUTC($obj->lastupdate,$CURRENT_USER->timezone);
	if ($sprjlist[$obj->subprj]!==false) {
		$obj->url = VIEW_SVN_URL . $prj->path . str_replace('$LANG',$team->lang,$sprjlist[$obj->subprj]->path) ."/". $obj->name;
	}
	$tuns+=$obj->untranslated;
	$tfuz+=$obj->fuzzy;
	$ttran+=$obj->translated;
	array_push($flist,$obj);
}

$astatus = $FILE_STATUS_LIST;

if ($CURRENT_USER->groupid == GROUP_USER) {
	$astatus = array(FILE_STATUS_CHKPND,FILE_STATUS_DONE);
}

$users=array();

$user = new user;
$user->teamid = $currentteam;
$userlist = $loader->loadlistBy($user,$user->listByFilters());

foreach ($userlist as $key => $obj) {
	array_push($users,$obj->userid);
}

$fpath="";
if ($data['fpath']!="") {
	foreach ($sprjlist as $obj) {
		if ($obj->id==$data['fpath']) {
			$fpath = array("id" => $obj->id,"name" => $obj->name);
			break;
		}
	}	
}

$next = $data['currentpage']+$data['recperpage'];
$back = $data['currentpage']-$data['recperpage'];

$tm->assign('recperpage',$data['recperpage']);
$tm->assign('currentpage',$data['currentpage']);

if ($next >= $totalrecords) {
	$tm->assign('nextpage',"none");	
} else {
	$tm->assign('nextpage',$next);
} 
if ($data['currentpage']==1) {
	$tm->assign('prevpage',"none");
} else {
	$tm->assign('prevpage',$back);
}

$maxcurrent = $data['recperpage']+($data['currentpage']-1);
if ($maxcurrent>$totalrecords) {
	$maxcurrent = $totalrecords;
}

$ar = array($data['currentpage'],$maxcurrent,$totalrecords);
$tm->assign('pagetext',createMessage($LANG['totalrecord'],$ar));

$tm->assign('list',$flist);
$tm->assign('statuslist',$FILE_STATUS_LIST);
$tm->assign('astatuslist',$astatus);
$tm->assign('userlist',$users);
$tm->assign('projectmessage',$pmessage);
$tm->assign('totaluntranslated',$tuns);
$tm->assign('totalfuzzy',$tfuz);
$tm->assign('totalstrings',$tfuz+$tuns+$ttran);
$tm->assign('projectname',$_SESSION['projectname']);
$tm->assign('projectid',$_SESSION['projectid']);
$tm->assign('pathlist',$sprjlist);
$tm->assign('order',$data['forder']);
$tm->assign('statusid',$data['fstatusid']);
$tm->assign('userid',$data['fuserid']);
$tm->assign('myfiles',$data['fmyfiles']);
$tm->assign('filestate',$data['ffilestate']);
$tm->assign('path',$fpath);

?>